#include "pch.hpp"
#include "CssHighlighter.hpp"

using namespace Engine;
using namespace Engine::Edit;




CssHighlighter::CssHighlighter(QTextDocument *document, ErrorModel::const_ptr errors)
	: BasicCodeHighlighter(document)
	, m_errors(errors)
{
	Rule rule;
	QTextCharFormat fmt;
	QStringList keywords;

	keywords << "\\btext\\b" << "\\bconsole\\b" << "\\bdesktop\\b" << "\\bpush-button\\b"
		<< "\\bframe\\b" << "\\bline-edit\\b" << "\\bprogress-bar\\b" << "\\bwindow\\b" << "\\bdialog\\b";
	add(keywords, Class);
	keywords.clear();

	keywords << "\\bbackground-color\\b" << "\\bbackground-image\\b" << "\\bbackground-image-l\\b"
		<< "\\bbackground-image-tl\\b" << "\\bbackground-image-t\\b" << "\\bbackground-image-tr\\b"
		<< "\\bbackground-image-r\\b" << "\\bbackground-image-br\\b" << "\\bbackground-image-b\\b"
		<< "\\bbackground-image-bl\\b" << "\\bforeground-color\\b" << "\\bforeground-image\\b"
		<< "\\bforeground-image-l\\b" << "\\bforeground-image-tl\\b" << "\\bforeground-image-t\\b"
		<< "\\bforeground-image-tr\\b" << "\\bforeground-image-r\\b" << "\\bforeground-image-br\\b"
		<< "\\bforeground-image-b\\b" << "\\bforeground-image-bl\\b" << "\\bfont\\b"
		<< "\\bfont-color\\b" << "\\bfont-style\\b" << "\\bfont-size\\b"
		<< "\\bfont-weight\\b" << "\\bmin-width\\b" << "\\bmax-width\\b"
		<< "\\bmin-height\\b" << "\\bmax-height\\b" << "\\bresizing\\b";
	add(keywords, Attribute);
	keywords.clear();

	keywords << "\\bhovered\\b" << "\\bactive\\b";
	add(keywords, Attribute);
	keywords.clear();
}
///////////////////////////////////////////////////////////////////////////////////////////////////

CssHighlighter::~CssHighlighter()
{}
///////////////////////////////////////////////////////////////////////////////////////////////////




QTextCharFormat CssHighlighter::format(Type type) const
{
	QTextCharFormat fmt = __super::format(Default);

	switch(type)
	{
	case Class:
		fmt.setForeground(QColor(145, 25, 10));
		break;
	case Attribute:
		fmt.setForeground(QColor(255, 53, 43));
		break;
	case State:
		fmt.setForeground(QColor(0, 0, 255));
		break;

	default:
		throw invalid_value_exception(type);
	}

	return fmt;
}
///////////////////////////////////////////////////////////////////////////////////////////////////

void CssHighlighter::add(QStringList keywords, Type type)
{
	Rule rule;
	auto fmt = format(type);

	foreach(QString keyword, keywords)
	{
		rule.pattern = QRegExp(keyword);
		rule.format  = fmt;
		__super::add(rule);
	}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
